<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Subscriptions_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function email_subs_users(){
		$total = count( $this->db->where('email_subscription','yes')->get(TBL_USER)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/subscriptions/email_subscription", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('email_subscription','yes')->limit($records_per_page, $per_page )->get(TBL_USER)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function sms_subscription_users(){
		$total = count( $this->db->where('sms_subscription','yes')->get(TBL_USER)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/subscriptions/sms_subscription", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('sms_subscription','yes')->limit($records_per_page, $per_page )->get(TBL_USER)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	function email_subs_submit(){
		$USER = $this->user_entity->init('email', $_POST['email']);
		if(!$USER){
			echo "Email is not valid.";exit();
		}
		$USER->setEmailSubscription('yes');
		$USER->update();
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();	
	}
	
	function sms_subs_submit(){
		$USER = $this->user_entity->init('username', $_POST['username']);
		if(!$USER){
			echo "Username is not valid.";exit();
		}
		if($USER->getCountry() != 'India'){
			echo "SMS subscription only apply for users of India.";exit();
		}
		$USER->setSmsSubscription('yes');
		$USER->update();
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();	
	}
	
	function unsubscription_user_email(){
		$USER = $this->user_entity->init('id', $_POST['user_id']);
		if(!$USER){
			exit();
		}
		$USER->setEmailSubscription('');
		$USER->update();
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();	
	}
	
	function unsubscription_user_sms(){
		$USER = $this->user_entity->init('id', $_POST['user_id']);
		if(!$USER){
			exit();
		}
		$USER->setSmsSubscription('');
		$USER->update();
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();	
	}
	
	function email_compose(){
		$total = count( $this->db->where('type', 'email')->get(TBL_EMAIL_SUBS)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/subscriptions/email_compose", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('type', 'email')->order_by('created_date','desc')->limit($records_per_page, $per_page )->get(TBL_EMAIL_SUBS)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function sms_compose(){
		$total = count( $this->db->where('type', 'sms')->get(TBL_EMAIL_SUBS)->result() );
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/subscriptions/sms_compose", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
							
		$records = $this->db->where('type', 'sms')->order_by('created_date','desc')->limit($records_per_page, $per_page )->get(TBL_EMAIL_SUBS)->result();
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function email_compose_form_submit(){
		$id = $_POST['id'];
		$subject = $this->input->post('subject',TRUE);
		$body = $this->input->post('body',TRUE);
		
		if(!$subject || !$body){
			echo "Subject or Body field must not empty.";exit();
		}
		if($id == 0){
			$newId = $this->email_subscription_entity->createNewEntity();
			$EMAIL = $this->email_subscription_entity->init('id',$newId);
			$EMAIL->setSubject($subject);
			$EMAIL->setBody($body);
			$EMAIL->setSent('');
			$EMAIL->setType('email');
			$EMAIL->setLastSentDate(0);
			$EMAIL->update();
		}else{
			$EMAIL = $this->email_subscription_entity->init('id',$id);
			$EMAIL->setSubject($subject);
			$EMAIL->setBody($body);
			$EMAIL->update();
		}
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function sms_compose_form_submit(){
		$id = $_POST['id'];
		$content = $this->input->post('content',TRUE);
		if(!$content){
			echo "Content must not empty.";exit();
		}
		if($id == 0){
			$newId = $this->email_subscription_entity->createNewEntity();
			$EMAIL = $this->email_subscription_entity->init('id',$newId);
			$EMAIL->setSmsContent($content);
			$EMAIL->setSent('');
			$EMAIL->setType('sms');
			$EMAIL->setLastSentDate(0);
			$EMAIL->update();
		}else{
			$EMAIL = $this->email_subscription_entity->init('id',$id);
			$EMAIL->setSmsContent($content);
			$EMAIL->update();
		}
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function email_compose_form_send(){
		$id = $_POST['id'];
		$subject = $this->input->post('subject',TRUE);
		$body = $this->input->post('body',TRUE);
		
		if(!$subject || !$body){
			echo "Subject or Body field must not empty.";exit();
		}
		if($id == 0){
			$newId = $this->email_subscription_entity->createNewEntity();
			$EMAIL = $this->email_subscription_entity->init('id',$newId);
			$EMAIL->setSubject($subject);
			$EMAIL->setBody($body);
			$EMAIL->setSent('');
			$EMAIL->setType('email');
			$EMAIL->setLastSentDate(0);
			$EMAIL->update();
		}else{
			$EMAIL = $this->email_subscription_entity->init('id',$id);
			$EMAIL->setSubject($subject);
			$EMAIL->setBody($body);
			$EMAIL->update();
		}
		
		foreach($this->getListUserEmailSubscription() as $email){
			$this->email_sender->setToEmail($email);
			$this->email_sender->setSubject($subject);
			$this->email_sender->setBody($body);
			
			$this->email_sender->sendEmail();
		}
		$EMAIL->setSent('Sent');
		$EMAIL->setLastSentDate(time());
		$EMAIL->update();
		
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function sms_compose_form_send(){
		$id = $_POST['id'];
		$content = $this->input->post('content',TRUE);
		if(!$content){
			echo "Content must not empty.";exit();
		}
		 
		if($id == 0){
			$newId = $this->email_subscription_entity->createNewEntity();
			$EMAIL = $this->email_subscription_entity->init('id',$newId);
			$EMAIL->setSmsContent($content);
			$EMAIL->setSent('');
			$EMAIL->setType('sms');
			$EMAIL->setLastSentDate(0);
			$EMAIL->update();
		}else{
			$EMAIL = $this->email_subscription_entity->init('id',$id);
			$EMAIL->setSmsContent($content);
			$EMAIL->update();
		}
		
		foreach($this->getListUserSmsSubscription() as $mobile){
			sendSMS($mobile, $content);
		}
		$EMAIL->setSent('Sent');
		$EMAIL->setLastSentDate(time());
		$EMAIL->update();
		
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function send_email(){
		$id = $_POST['id'];
		$EMAIL = $this->email_subscription_entity->init('id',$id);
		$subject = $EMAIL->getSubject();
		$body = $EMAIL->getBody();
		foreach($this->getListUserEmailSubscription() as $email){
			$this->email_sender->setToEmail($email);
			$this->email_sender->setSubject($subject);
			$this->email_sender->setBody($body);
			
			$this->email_sender->sendEmail();
		}
		$EMAIL->setSent('Sent');
		$EMAIL->setLastSentDate(time());
		$EMAIL->update();
		
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function send_sms(){
		$id = $_POST['id'];
		$EMAIL = $this->email_subscription_entity->init('id',$id);
		$content = $EMAIL->getSmsContent();
		foreach($this->getListUserSmsSubscription() as $mobile){
			sendSMS($mobile, $content);
		}
		$EMAIL->setSent('Sent');
		$EMAIL->setLastSentDate(time());
		$EMAIL->update();
		
		echo 'ok';
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function delete_email(){
		$id = $_POST['id'];
		$this->db->where('id',$id)->delete(TBL_EMAIL_SUBS);
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function delete_sms(){
		$id = $_POST['id'];
		$this->db->where('id',$id)->delete(TBL_EMAIL_SUBS);
		$this->session->set_flashdata('success', lang('dataman.success'));
		exit();
	}
	
	function getListUserEmailSubscription(){
		$records = $this->db->where('email_subscription','yes')->get(TBL_USER)->result();
		$array = array();
		foreach($records as $item){
			$array[] = $item->email;
		}
		return $array;
	}
	
	function getListUserSmsSubscription(){
		$records = $this->db->where('sms_subscription','yes')->get(TBL_USER)->result();
		$array = array();
		foreach($records as $item){
			$array[] = $item->mobile;
		}
		return $array;
	}
	
//end class	
}	
